package com.rtx.wcs.ess.ristowcs;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rtx.edi.platform.synctask.service.ConvertService;
import com.rtx.wcs.std.bean.po.TaskDetail;
import com.rtx.wcs.std.handle.ristowcs.mvtask.AbsMVTaskDownHandle;
import com.rtx.wcs.std.service.RtxPalletcantileverService;


/**
 * 移动功能【库存查询-立库箱移出】【库存查询-无指令入库】
 * @author Administrator
 *
 */
@Service("MV-TASK")
public class MVTASK extends AbsMVTaskDownHandle{
	
	Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	RtxPalletcantileverService rtxPalletcantileverService;
	
	@Value("${ess.task.down.url}")
	private String RF_CTU_URL;
	
	@Autowired
	ConvertService convertService;
	

	@Override
	protected List<TaskDetail> queryToSendDataByRISWmsCondition(String request) {
		List<TaskDetail> list = super.queryToSendDataByRISWmsCondition(request);
		list.forEach(temp->temp.setTaskcode(UUID.randomUUID().toString()));
		return list;
	}


	//	{
	//		"code": 0,
	//		"msg": "success",
	//		"data": {
	//			"taskCode-001": "OK"
	//		}
	//	}
	@Override
	protected Map<String, String> handleResponse(TaskDetail o, String response,String mappingcode) {
		Map<String,String> resultMap = new HashMap<>();
		resultMap.put("editflag", "");//PS为成功 PE为失败
		resultMap.put("message", "");
		resultMap.put("data", "");
		resultMap.put("status", "");
		
		try {
			JSONObject res = JSON.parseObject(response);
			if("0".equals(res.getString("code"))) {
				resultMap.put("editflag", "PS");
			}else {
				resultMap.put("editflag", "PE");
			}
			resultMap.put("message", res.getString("msg"));
			resultMap.put("data", o.getTaskcode());
		} catch (Exception e) {
			logger.error("",e);
			resultMap.put("message", response);
			resultMap.put("editflag", "PE");
		}finally {
			resultMap.put("data", o.getTaskcode());
		}
		return resultMap;
	}



	//示例报文
	//	{
	//		"taskType": "putaway",
	//		"taskGroupCode": "taskGroupCode-001",
	//		"groupPriority": 0,
	//		"tasks": [{
	//			"taskCode": "taskCode-001",
	//			"taskPriority": 0,
	//			"taskDescribe": {
	//				"containerCode": "Container001",
	//				"containerType": "CT_KUBOT_STANDARD",
	//				"storageTag": "",
	//				"fromLocationCode": "S07-001-008-02",
	//				"toStationCode": "Aport_station_1",
	//				"toLocationCode": "A-001-001-01"
	//			}
	//		}]
	//	}
	@Override
	protected JSONObject converWcsRequest(TaskDetail t,String mappingcode) {
		JSONObject edirequest = new JSONObject();
		edirequest.put("taskType", "carry");
		edirequest.put("taskGroupCode", t.getRtxtaskgrp());
		edirequest.put("groupPriority", "0");
		
		JSONArray tasks = new JSONArray();
		JSONObject task = new JSONObject();
		task.put("taskCode", "");
		task.put("taskPriority", "");
		
		JSONObject taskDescribe = new JSONObject();
		taskDescribe.put("containerCode", t.getFromid());
		taskDescribe.put("containerType", "CT_KUBOT_STANDARD");
		taskDescribe.put("storageTag", "");
		taskDescribe.put("fromLocationCode", t.getFromloc());
		taskDescribe.put("toStationCode", t.getToloc());
		taskDescribe.put("toLocationCode", "");
		
		tasks.add(task);
		edirequest.put("tasks", tasks);
		return edirequest;
	}


	@Override
	protected String getEdiRequestURL(String mappingcode) {
		return RF_CTU_URL;
	}

}
